package bookStore.dao.business;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import bookStore.dao.DaoTemplate;
import bookStore.po.Author;

public class AuthorDaoImpl extends DaoTemplate implements AuthorDao{

	public AuthorDaoImpl(Connection connection) {
		super(connection);
		
	}

	@Override
	public List<Author> queryAll(int offset, int pageSize) {
		this.sql="select id,name from author limit ?,?";
		 try {	
			List<Author> list=  this.queryRunner.query(this.connection, this.sql,new BeanListHandler<Author>(Author.class),offset,pageSize);
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}  
		return null;
	}

	@Override
	public long count() {
		return 0;
	}

	@Override
	public void save(Author t) {
	}

	@Override
	public Author get(int id) {
		this.sql="select * from author where id=?";
		try {
			return this.queryRunner.query(connection, sql, new BeanHandler<Author>(Author.class), id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void update(Author t) {
	}

	@Override
	public void delete(int id) {
	}

	@Override
	public String getAuthorNameById(int authorId) {
		sql="select name from author where id=?";
		try {
			pstat=connection.prepareStatement(sql);
			pstat.setInt(1, authorId);
			rs=pstat.executeQuery();
			while(rs.next())
			{
				return rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

}
